package com.maishi.programmer.Stack;

import java.util.Stack;

/**
 * Created by brossdong on 2018/3/14.
 * 用两个栈实现一个队列，实现对了的两个函数 appendTail 和 deleteHead，
 * 分别完成在队列尾插入结点和在队列头部删除结点的功能。
 */
public class StackProblem7<T> {

    /**
     * stack1 进行压栈
     * stack2 进行出栈
     */
    //先定义两个栈
    Stack<T> stack1 = new Stack<T>();
    Stack<T> stack2 = new Stack<T>();

    public void append(T t) {
        stack1.push(t);
    }

    public T delete() throws Exception {
        if (stack2.isEmpty()) {
            while (!stack1.isEmpty()) {
                stack2.push(stack1.pop());
            }
        }
        if (stack2.isEmpty()) {
            throw new Exception("队列不能为空");
        }
        return stack2.pop();
    }

    public static void main (String[] args) throws Exception {
        StackProblem7<String> p7=new StackProblem7<>();
        p7.append("1");
        p7.append("2");
        p7.append("3");
        System.out.println(p7.toString());
        p7.delete();
    }

}
